﻿using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace PathFind.Algo {
    public enum Direction {
        S = 0,
        SE = 1,
        E = 2,
        NE = 3,
        N = 4,
        NW = 5,
        W = 6,
        SW = 7,
        CENTER = 8,
        START = S,
        END = CENTER,
        CIRCLE_NUM = CENTER,
    }

    public static class DirectionUtil {
        // https://www.redblobgames.com/pathfinding/a-star/implementation.html#ties-checkerboard-neighbors
        // Here’s the hack:in the graph class, make the list of neighbors depend on (x + y) % 2
        public static  Direction[,] dirsMap = {
            {
                Direction.S,
                Direction.N,
                Direction.W,
                Direction.E,

                Direction.SE,
                Direction.NW,
                Direction.NE,
                Direction.SW,
            },
            {
                Direction.E,
                Direction.W,
                Direction.N,
                Direction.S,

                Direction.SW,
                Direction.NE,
                Direction.NW,
                Direction.SE,
            },
        };

        public static int dirLen = dirsMap.GetLength(1);
    }

    public class DirectionSet {
        private readonly BitArray dirArray;
        public DirectionSet() {
            dirArray = new BitArray((int)Direction.CIRCLE_NUM, false);
        }

        public void Clear() {
            dirArray.SetAll(false);
        }

        public void SetAll(bool b) {
            dirArray.SetAll(b);
        }

        public void AddDirectionToSet(Direction dir) {
            dirArray.Set((int) dir, true);
        }

        public bool IsDirectionSet(Direction dir) {
            return dirArray.Get((int) dir);
        }

        public void MergeDirectionSet(DirectionSet dSet) {
            dirArray.Or(dSet.dirArray);
        }

        public override string ToString() {
            StringBuilder sb = new StringBuilder();
            sb.Append("(");
            foreach (bool isSet in dirArray) {
                if (isSet) {
                    sb.Append("T");
                }
                else {
                    sb.Append("F");
                }
            }

            sb.Append(")");
            return sb.ToString();
        }
    }
}